package cn.csc.module.order.job;

import cn.csc.framework.tenant.core.job.TenantJob;
import cn.csc.module.order.controller.admin.order.vo.OrderReqVO;
import cn.csc.module.order.dal.dataobject.item.OrderItemDO;
import cn.csc.module.order.enums.OrderStatusEnum;
import cn.csc.module.order.service.item.OrderItemService;
import cn.csc.module.order.service.order.OrderService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;


/**
 * @Author：yequnli
 * @Date：2024/1/16 18:49
 */
@Component
@TenantJob // 多租户
@Slf4j
public class OrderJob {

    @Resource
    private OrderService orderService;

    @Resource
    private OrderItemService orderItemService;

    /**
     * 自发货时间10天后，如买家没有点击收货，系统自动收货，00点执行
     * 0 10 0 * * ?
     * @return
     */
    @XxlJob("receiptJob")
    public ReturnT<String> receiptJob() {
        log.info("[receiptJob]进入自动收货---");
        OrderReqVO reqVO = new OrderReqVO();
        reqVO.setOrderStatus(OrderStatusEnum.CONSIGNMENT.value());
        //当前时间-10 天等于订单的发货时间，处理自动收货
        LocalDateTime localDate = LocalDate.now().atStartOfDay();
        reqVO.setDeliveryTime(localDate.plusDays(-10));// 测试环境先改成1天，上线后改回10天
        log.info("[receiptJob]reqVO:{}",reqVO);
        List<OrderItemDO> list = orderItemService.getListByDeliveryTime(reqVO);

        int count = orderService.autoReceive(list);
        log.info("[receiptJob] {}", String.format("系统自动收货 %s 条", count));
        return new ReturnT<>(String.format("系统自动收货 %s 个", count));
    }


}
